package com.swpu.cbg.springcard;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXWebpageObject;

import com.tencent.mm.sdk.openapi.WXTextObject;


public class MainActivity extends Activity implements View.OnClickListener {

    private int flag = 1;
    private static final String APP_KEY = "";
    private static final int INTENT_REQUEST_CODE = 1000;
    private ImageView mImage;
    private EditText mContent;
    private Button mShare;
    private IWXAPI api;
    private byte[] sahreImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWX();
        initView();
    }

    private void initWX() {
        api = WXAPIFactory.createWXAPI(this, APP_KEY);
        api.registerApp(APP_KEY);
    }

    private void initView() {
        mImage = (ImageView) findViewById(R.id.id_image);
        mContent = (EditText) findViewById(R.id.id_word);
        mShare = (Button) findViewById(R.id.id_sure);
        mContent.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/test.ttf"));
        mImage.setOnClickListener(this);
        mShare.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_image:
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, INTENT_REQUEST_CODE);
                break;


            case R.id.id_sure:
                wxShare();
                mShare.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void wxShare() {
        WXWebpageObject wxWebpageObject = new WXWebpageObject();
        WXMediaMessage message = new WXMediaMessage(wxWebpageObject);
        message.mediaObject = new WXImageObject(getSahreImage());
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = message;
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && resultCode == INTENT_REQUEST_CODE) {
            if (data != null) {
                mImage.setImageURI(data.getData());
            }
        }
    }

    public Bitmap getSahreImage() {
        mShare.setVisibility(View.INVISIBLE);
        //获取屏幕主视图
        View view = getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        return view.getDrawingCache();
    }
}
